<?php

include '../inc/db_util.php';
include '../ws/angkatan_controller.php';

$action = $_GET['act'];

switch ($action) {
    case 'get':
        read();
        break;
    case 'add':
        create();
        break;
    case 'upd':
        update();
        break;
    case 'del':
        delete();
        break;
    default:
        echo 'Wrong command!';
        break;
}

function read() {
    $page = isset($_POST['page']) ? intval($_POST['page']) : 1;
    $rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
    $sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'nim';
    $order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';
    $cari = isset($_POST['cari']) ? mysql_real_escape_string($_POST['cari']) : '';

    $offset = ($page - 1) * $rows;

    $where = " WHERE nim LIKE '%$cari%' OR nama LIKE '%$cari%' OR alamat LIKE '%$cari%' "
            . " OR telepon LIKE '%$cari%' OR email LIKE '%$cari%' OR tahun_angkatan = '$cari' ";

    $quey = "SELECT * FROM tb_mahasiswa m
        LEFT JOIN tb_angkatan a ON a.id_angkatan = m.id_angkatan
	$where ";

    $paging = " ORDER BY $sort $order LIMIT $rows OFFSET $offset";

    $result = array();
    $count = mysql_query($quey) or die(mysql_error());
    $result['total'] = mysql_num_rows($count);
    $row = array();

    $criteria = mysql_query($quey . $paging) or die(mysql_error());

    while ($data = mysql_fetch_array($criteria)) {
        $row[] = array(
            'id' => $data['id_mahasiswa'],
            'nim' => $data['nim'],
            'nama' => $data['nama'],
            'alamat' => $data['alamat'],
            'telepon' => $data['telepon'],
            'email' => $data['email'],
            'angkatan' => $data['id_angkatan'],
            'tahun' => $data['tahun_angkatan'],
        );
    }
    $result = array_merge($result, array('rows' => $row));
    echo json_encode($result);
}

function create() {
    $nim = $_POST['nim'];
    $nama = $_POST['nama'];
    $alamat = $_POST['alamat'];
    $telepon = $_POST['telepon'];
    $email = $_POST['email'];
    $angkatan = $_POST['tahun'];
    
    $tahun = getTahunById($angkatan);
    $id_mhs = $tahun . substr($nim, 2); // 2008 + substring (0804505080, 2) = 200804505080
    
    $quey = "SELECT * FROM tb_mahasiswa WHERE nim='$nim'";
    $row = mysql_query($quey) or die(mysql_error());
    $count = mysql_num_rows($row);

    if ($count < 1) {
        $query = "INSERT INTO tb_mahasiswa VALUES('$id_mhs', '$nim','$nama',"
                . "'1234567890',md5(1234567890),'$alamat','$telepon','$email', '$angkatan')";

        $result = mysql_query($query) or die(mysql_error());
        if ($result) {
            echo "$nama telah di simpan";
        }
    } else {
        echo "NIM $nim sudah ada!";
    }
}

function update() {
    $id = $_POST['id'];
    $nim = $_POST['nim'];
    $nama = $_POST['nama'];
    $alamat = $_POST['alamat'];
    $telepon = $_POST['telepon'];
    $email = $_POST['email'];
    $angkatan = $_POST['tahun'];

    $query = "UPDATE tb_mahasiswa SET nama='$nama', alamat='$alamat',
                    telepon='$telepon', email='$email', id_angkatan='$angkatan', nim='$nim'
                    WHERE id_mahasiswa='$id'";

    $result = mysql_query($query) or die(mysql_error());
    if ($result) {
        echo "$nama telah di update";
    }
}

function delete() {
    $nim = $_POST['nim'];

    $array = json_decode($nim);
    $nims = implode("','", $array);

    $query = "DELETE FROM tb_mahasiswa WHERE nim IN ('" . $nims . "')";
    $result = mysql_query($query) or die(mysql_error());

    $count = count($array) > 1 ? "(" . count($array) . ") " : "";

    if ($result) {
        echo $count . "Data yand di pilih telah dihapus";
    } else {
        echo "Gagal menghapus " . $count . " data yang di pilih!";
    }
}

?>